package com.qf.controller;

import com.qf.respon.ResponseData;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
@author fu
@date 2022/3/1822:31
*/
@WebServlet("/check")
public class CheckUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1)获取前台用户提交的参数
        String username = request.getParameter("username");
        //System.out.println(username);
        //2)调用业务接口
        UserService userService = new UserServiceImpl() ;
        boolean flag = userService.checkUser(username);

        //封装ResponseData实体
        ResponseData responseData = new ResponseData() ;
        if(flag){
            //成立,可以注册
            responseData.setCode(1);
            responseData.setMsg("正确");

        }else{
            //不成立,不可以注册
            responseData.setCode(0);
            responseData.setMsg("不可用");
        }

        //无论上面成立,不成立,最终将当前的responseData响应json格式过去
        String jsonStr = JsonUtils.object2json(responseData);
        response.getWriter().write(jsonStr);
    }
}
